#! /usr/bin/env bash
set -e

service postgresql start

# Calculate params for AWS Batch
if [ ! -z "$AWS_BATCH_JOB_ARRAY_INDEX" ]; then
	# The batch should have three env variables:
	# * BATCH_START_LEDGER - start ledger of the job, must be equal 1 or a
	#   checkpoint ledger (i + 1) % 64 == 0.
	# * BATCH_SIZE - size of the batch in ledgers, must be multiple of 64!
	# * BRANCH - git branch to build
	#
	# Ex: BATCH_START_LEDGER=63, BATCH_SIZE=64 will create the following ranges:
	# AWS_BATCH_JOB_ARRAY_INDEX=0: [63, 127]
	# AWS_BATCH_JOB_ARRAY_INDEX=1: [127, 191]
	# AWS_BATCH_JOB_ARRAY_INDEX=2: [191, 255]
	# AWS_BATCH_JOB_ARRAY_INDEX=3: [255, 319]
	# ...

	if [ $BATCH_START_LEDGER -eq 1 ]; then
		export FROM=`expr "$BATCH_SIZE" \* "$AWS_BATCH_JOB_ARRAY_INDEX" + "$BATCH_START_LEDGER" - "2"`
		export TO=`expr "$BATCH_SIZE" \* "$AWS_BATCH_JOB_ARRAY_INDEX" + "$BATCH_START_LEDGER" + "$BATCH_SIZE" - "2"`
		if [ $FROM -eq -1 ]; then
			export FROM="1"
		fi
	else
		export FROM=`expr "$BATCH_SIZE" \* "$AWS_BATCH_JOB_ARRAY_INDEX" + "$BATCH_START_LEDGER"`
		export TO=`expr "$BATCH_SIZE" \* "$AWS_BATCH_JOB_ARRAY_INDEX" + "$BATCH_START_LEDGER" + "$BATCH_SIZE"`
	fi
fi

export LEDGER_COUNT=`expr "$TO" - "$FROM" + "1"`

echo "FROM: $FROM TO: $TO"

dump_horizon_db() {
	echo "dumping history_effects"
	psql "postgres://postgres:postgres@localhost:5432/horizon?sslmode=disable" -t -A -F"," --variable="FETCH_COUNT=100" -c "select history_effects.history_operation_id, history_effects.order, type, details, history_accounts.address from history_effects left join history_accounts on history_accounts.id = history_effects.history_account_id order by history_operation_id asc, \"order\" asc" > "${1}_effects"
	echo "dumping history_ledgers"
	psql "postgres://postgres:postgres@localhost:5432/horizon?sslmode=disable" -t -A -F"," --variable="FETCH_COUNT=100" -c "select sequence, ledger_hash, previous_ledger_hash, transaction_count, operation_count, closed_at, id, total_coins, fee_pool, base_fee, base_reserve, max_tx_set_size, protocol_version, ledger_header, successful_transaction_count, failed_transaction_count from history_ledgers order by sequence asc" > "${1}_ledgers"
	echo "dumping history_operations"
	psql "postgres://postgres:postgres@localhost:5432/horizon?sslmode=disable" -t -A -F"," --variable="FETCH_COUNT=100" -c "select * from history_operations order by id asc" > "${1}_operations"
	echo "dumping history_operation_participants"
	psql "postgres://postgres:postgres@localhost:5432/horizon?sslmode=disable" -t -A -F"," --variable="FETCH_COUNT=100" -c "select history_operation_id, address from history_operation_participants left join history_accounts on history_accounts.id = history_operation_participants.history_account_id order by history_operation_id asc, address asc" > "${1}_operation_participants"
	echo "dumping history_trades"
	psql "postgres://postgres:postgres@localhost:5432/horizon?sslmode=disable" -t -A -F"," --variable="FETCH_COUNT=100" -c "select history_trades.history_operation_id, history_trades.order, history_trades.ledger_closed_at, history_trades.offer_id, CASE WHEN history_trades.base_is_seller THEN history_trades.price_n ELSE history_trades.price_d END, CASE WHEN history_trades.base_is_seller THEN history_trades.price_d ELSE history_trades.price_n END, CASE WHEN history_trades.base_is_seller THEN history_trades.base_offer_id ELSE history_trades.counter_offer_id END, CASE WHEN history_trades.base_is_seller THEN history_trades.counter_offer_id ELSE history_trades.base_offer_id END, CASE WHEN history_trades.base_is_seller THEN baccount.address ELSE caccount.address END, CASE WHEN history_trades.base_is_seller THEN caccount.address ELSE baccount.address END, CASE WHEN history_trades.base_is_seller THEN basset.asset_type ELSE casset.asset_type END, CASE WHEN history_trades.base_is_seller THEN basset.asset_code ELSE casset.asset_code END, CASE WHEN history_trades.base_is_seller THEN basset.asset_issuer ELSE casset.asset_issuer END, CASE WHEN history_trades.base_is_seller THEN casset.asset_type ELSE basset.asset_type END, CASE WHEN history_trades.base_is_seller THEN casset.asset_code ELSE basset.asset_code END, CASE WHEN history_trades.base_is_seller THEN casset.asset_issuer ELSE basset.asset_issuer END from history_trades left join history_accounts baccount on baccount.id = history_trades.base_account_id left join history_accounts caccount on caccount.id = history_trades.counter_account_id left join history_assets basset on basset.id = history_trades.base_asset_id left join history_assets casset on casset.id = history_trades.counter_asset_id order by history_operation_id asc, \"order\" asc" > "${1}_trades"
	echo "dumping history_transactions"
	psql "postgres://postgres:postgres@localhost:5432/horizon?sslmode=disable" -t -A -F"," --variable="FETCH_COUNT=100" -c "select transaction_hash, ledger_sequence, application_order, account, account_sequence, max_fee, operation_count, id, tx_envelope, tx_result, tx_meta, tx_fee_meta, signatures, memo_type, memo, time_bounds, successful, fee_charged from history_transactions order by id asc" > "${1}_transactions"
	echo "dumping history_transaction_participants"
	psql "postgres://postgres:postgres@localhost:5432/horizon?sslmode=disable" -t -A -F"," --variable="FETCH_COUNT=100" -c "select history_transaction_id, address from history_transaction_participants left join history_accounts on history_accounts.id = history_transaction_participants.history_account_id order by history_transaction_id, address" > "${1}_transaction_participants"
}

stellar-core new-db
stellar-core catchup $TO/$LEDGER_COUNT

# pubnet horizon config
export NETWORK_PASSPHRASE="Public Global Stellar Network ; September 2015"
export HISTORY_ARCHIVE_URLS="https://s3-eu-west-1.amazonaws.com/history.stellar.org/prd/core-live/core_live_001"
export DATABASE_URL="postgres://postgres:postgres@localhost:5432/horizon?sslmode=disable"
export INGEST="false"
export ENABLE_EXPERIMENTAL_INGESTION="true"
export INGEST_FAILED_TRANSACTIONS=true
export STELLAR_CORE_URL="http://localhost:11626"
export STELLAR_CORE_DATABASE_URL="postgres://postgres:postgres@localhost:5432/core?sslmode=disable"

cd stellar-go
git pull origin
if [ ! -z "$BRANCH" ]; then
	git checkout $BRANCH
fi
git log -1

/usr/local/go/bin/go build -v ./services/horizon
./horizon db migrate up
./horizon expingest verify-range --from $FROM --to $TO --verify-state

if [ ! -z "$VERIFY_HISTORY" ]; then
	dump_horizon_db "exp_history"

	echo "Done dump_horizon_db exp_history"

	psql "postgres://postgres:postgres@localhost:5432/horizon?sslmode=disable" -c "DROP SCHEMA public CASCADE;"
	psql "postgres://postgres:postgres@localhost:5432/horizon?sslmode=disable" -c "CREATE SCHEMA public;"

	# sudo -u postgres dropdb horizon
	# sudo -u postgres createdb horizon

	git checkout h_0.24.1_v2_meta

	/usr/local/go/bin/go build -v ./services/horizon

	export INGEST="true"
	export ENABLE_EXPERIMENTAL_INGESTION="false"

	./horizon db migrate up
	# We ignore the first ledger when reingesting using legacy ingestion because
	# verify range doesn't ingest history for the first ledger. $FROM ledger from
	# this job will be checked because it's $TO of the previous job.
	export FROM_LEGACY=`expr "$FROM" + "1"`
	./horizon db reingest range $FROM_LEGACY $TO

	dump_horizon_db "legacy_history"
	echo "Done dump_horizon_db legacy_history"

	diff --speed-large-files legacy_history_effects exp_history_effects
	diff --speed-large-files legacy_history_ledgers exp_history_ledgers
	diff --speed-large-files legacy_history_operations exp_history_operations
	diff --speed-large-files legacy_history_operation_participants exp_history_operation_participants
	diff --speed-large-files legacy_history_trades exp_history_trades
	diff --speed-large-files legacy_history_transactions exp_history_transactions
	diff --speed-large-files legacy_history_transaction_participants exp_history_transaction_participants
fi

echo "OK"
